# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.addons.dm_base.utils import field_boolean_name_cn as boolean_name,field_selection_name as selection_name
from openerp.addons.dm_rubylong import get_rubylong_fields_xml,get_rublong_data_url
from openerp.osv import fields, osv
from datetime import datetime
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT

class purchase_order(osv.osv):
    
    _inherit="purchase.order"
    
    def _get_rubylong_xml_purchase_order(self, cr, uid, ids, field_names, args, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        #johnw, 2016/12/05, 避免出错, 重算金额
        self.recalc_amount(cr, uid, ids, context=context)            
        res = {}
        for order_id in ids:
            res[order_id] = ''
        data_xml = ''
        for order in self.browse(cr, uid, ids, context=context):
            #当前对象的xml
            data_xml = ''
            #header data
            order_fields = [
                        ('company_id.logo','company_logo'),
                        ('company_id.name','company_id_name'),
#                        #公司地址
                        ('company_id.street','company_id_street'),
                        ('company_id.street2','company_id_street2'),
                        ('company_id.city','company_id_city'),
                        ('company_id.state_id.name','company_id_state'),
                        ('company_id.country_id.name','company_id_country_name'),
                        #电话,传真
                        ('company_id.phone','company_id_phone'),
                        ('company_id.fax','company_id_fax'),
                        
                        #采购订单
                        'name',
                        #下单日期
                        'date_order',
                        
                        #供应商
                        ('partner_id.code','code'),
                        ('partner_id.name','partner_id'),
                        #地址(供应商)street,city,state_id,country_id
                        ('partner_id.street','street'),
                        ('partner_id.city','city'),
                        ('partner_id.state_id.name','state_id'),
                        ('partner_id.country_id.name','country_id'),
                        #联系人
                        ('partner_id.contact','contact'),
                        #电话
                        ('partner_id.mobile','mobile'),
                        ('partner_id.phone','phone'),
                        #传真
                        ('partner_id.fax','fax'),
                        #未含税金额
                        'amount_untaxed',
                        #税金
                        'amount_tax',
                        #总额
                        'amount_total',
                        #支付条款
                        ('payment_term_id.name','payment_term_id'),
                        #运输信息
                        ('transport_method.name','transport_method'),
                        ('freight_method.name','freight_method'),
                        #条款和条件
                        'notes',
                        #说明(备注)
                        'comments',
                        #制单
                        ('create_uid.name','create_uid'),
                        
                        ('id','order_id'), 
                        ]
            create_date = order.create_date and (fields.datetime.context_timestamp(cr, uid,
                                                        timestamp=datetime.strptime(order.create_date, DEFAULT_SERVER_DATETIME_FORMAT),
                                                         context=context)) or ''
                
            order_fields.append({'create_date':create_date})
            
            #税
            taxes_id_name = ','.join([tax.name for tax in order.taxes_id])
            order_fields.append({'taxes_id':taxes_id_name})

            data_xml += get_rubylong_fields_xml(order, 'header', order_fields)
            
            #detail data
            line_fields = [
                        #产品
                        ('product_id.default_code','default_code'),
                        ('product_id.name','product_id'),
                        ('product_id.variants', 'variants'),
                        ('product_id.part_no_external','part_no_external'),
                        #数量
                        'product_qty',
                        #单位
                        ('product_uom.name','product_uom'),
                        #交货日期
                        'date_planned',
                        #征集单价
                        'price_unit',
                        #说明
                        ('name','notes'),
                        
                        ('order_id.id','order_id'),   
                        ] 
            
            seq = 0 
            #实现按照物料编码升序排序
            order.order_line.sort(key=lambda line: line.product_id.default_code)
            for line in order.order_line:
                if line.state == 'cancel':
                    continue           
                seq += 1
                #小计
                amount_total = line.product_qty * line.price_unit
                line_fields_new = line_fields + [{'order_line':seq},{'amount_total':amount_total}]
                data_xml += get_rubylong_fields_xml(line, 'detail', line_fields_new)

            #write data to file            
            res[order.id] = get_rublong_data_url(order,data_xml,cr.dbname)
            
        return res            

    _columns = {
            'rubylong_xml_file':fields.function(_get_rubylong_xml_purchase_order, type='text', string='Rubylong xml'),
            }
    
#po_super.STATE_SELECTION = STATE_SELECTION_PO  